/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    TextInput,
    Platform,
    TouchableOpacity,
    View,
    Image
} from 'react-native';

import Toast from 'react-native-root-toast';
import Dimensions from 'Dimensions';

import Tabbar from './tabbar.js';
import CockpitGesturePassword from './CockpitGesturePassword.js';

let windowWidth = Dimensions.get('window').width;

export default class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
            curLoginName: '',
            curPassword: '',
        };
    }

    componentDidMount() {
        storage.load({//获取用户id
            key: 'user',
        }).then(ret => {
            //userId存在，获取用户对应的手势密码
            global.userid = ret.userid;
            global.userInfo = ret.userInfo;
            storage.load({
                key: 'GesturePassword' + global.userid
            }).then(ret => {
                //存在手势密码跳转到手势密码输入界面
                const { navigator } = this.props;
                if (navigator) {
                    navigator.push({
                        component: CockpitGesturePassword,
                    })
                }
            }).catch(err => {

            })

        }).catch(err => {
            ;
        });
    }

    _onPressLogin() {
        const { navigator } = this.props;
        if (navigator) {
            navigator.push({
                // component: Tabbar,
                component: CockpitGesturePassword,
            })
        }

        // if (this.state.curLoginName == '' || this.state.curPassword == '') {
        //     Toast.show("用户名或密码不能为空", { duration: Toast.durations.SHORT, position: Toast.positions.CENTER });
        //     return;
        // }

        // let m = cockpitGet.mobileLogin(this.state.curLoginName, this.state.curPassword);
        // m.then((response) => response.json())
        //     .then((responseJson) => {
        //         // console.warn(JSON.stringify(responseJson))
        //         if (responseJson.retcode == 0 || responseJson.retcode == '0') {
        //             //登录成功保存用户信息
        //             if (responseJson.result.userRelations.length == 0){
        //                 Toast.show("用户信息异常，请联系管理员", { duration: Toast.durations.SHORT, position: Toast.positions.CENTER });
        //                 return;
        //             }
        //             global.userInfo = responseJson.result;
        //             global.userid = userInfo.userRelations[0].userId;
        //             //保存用户信息
        //             storage.save({
        //                 key:'user',
        //                 rawData:{
        //                     userid: global.userid,
        //                     userInfo: global.userInfo
        //                 }
        //             })
        //             //登录成功跳转
        //             const { navigator } = this.props;
        //             if (navigator) {
        //                 navigator.push({
        //                     // component: Tabbar,
        //                     component: CockpitGesturePassword,
        //                 })
        //             }
        //         } else {
        //             Toast.show(responseJson.retmsg == null ? "登录失败" : responseJson.retmsg, { duration: Toast.durations.SHORT, position: Toast.positions.CENTER });
        //         }
        //     })
        //     .catch((error) => {
        //         // console.warn(JSON.stringify(error))
        //         //Toast.show(error == null ? "系统异常" : JSON.stringify(error), {duration: Toast.durations.SHORT, position: Toast.positions.CENTER});
        //         Toast.show("请检查网络设置", { duration: Toast.durations.SHORT, position: Toast.positions.CENTER });
        //     });
    }

    updateLoginName(text) {
        this.setState({
            curLoginName: text,
        });
    }

    updatePassword(text) {
        this.setState({
            curPassword: text,
        });
    }

    render() {
        return (
            <View style={styles.container}>
                {/*<Image source={require('../images/logo.png')}></Image>
                <Text style={styles.appNameStyle}>贷前调查</Text>*/}
                <TextInput
                    style={styles.TextInput}
                    placeholder="请输入用户名"
                    autoCorrect={false}
                    underlineColorAndroid='transparent'
                    onChangeText={(text) => this.updateLoginName(text)}
                    />
                <TextInput
                    style={styles.TextInput}
                    placeholder="请输入密码"
                    secureTextEntry={true}
                    autoCorrect={false}
                    underlineColorAndroid='transparent'
                    clearButtonMode='while-editing'
                    onChangeText={(text) => this.updatePassword(text)}
                    />
                <TouchableOpacity onPress={this._onPressLogin.bind(this)}>
                    <View style={styles.textLoginViewStyle}>
                        <Text style={styles.textLoginStyle}>登录</Text>
                    </View>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    TextInput: {
        backgroundColor: '#00000000',
        borderColor: '#12b7f5',
        borderWidth: 1,
        //设置圆角程度
        borderRadius: 5,
        width: windowWidth - 50,
        height: 45,
        paddingLeft: 10,
        paddingRight: 10,
        //根据不同平台进行适配
        marginTop: Platform.OS === 'ios' ? 5 : 10,
        marginLeft: 20,
        marginRight: 20,
    },
    textLoginViewStyle: {
        width: windowWidth - 100,
        height: 40,
        backgroundColor: '#12b7f5',
        borderRadius: 30,
        marginTop: 30,
        alignSelf: 'center',
        justifyContent: 'center',
        alignItems: 'center',
    },
    //登录Text文本样式
    textLoginStyle: {
        fontSize: 18,
        color: 'white',
    },
    appNameStyle: {
        marginTop: 10,
        marginBottom: 60,
        fontSize: 20
    }
});
